<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Member;
use think\exception\DbException;
use think\response\Json;

/**
 * 反馈管理
 *
 * @icon fa fa-circle-o
 */
class Feedback extends Backend
{

    /**
     * Member模型对象
     * @var \app\common\model\Member
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Washlog();
        $this->view->assign("statusList", $this->model->getStatusList());
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with(["member"=>function($query){
                $query->withField('id,nickname');
            }])
            ->where($where)
            ->where(["washlog.status"=>'1'])
            ->order($sort, $order)
            ->paginate($limit)
            ->each(function ($v){
                if($v["anonymous"]==1 || empty($v["member"]["nickname"])){
                    $v["member"]["nickname"]="匿名用户";
                }else{
                    $model=new Member();
                    $v["member"]["nickname"]=$model->emoji_decode($v["member"]["nickname"]);
                }
            });
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 处理反馈
     * */
    public function ok()
    {
        if (!$this->request->param("id")) $this->error('id为空');
        $param=$this->request->param();
        $row = $this->model->get($param["id"]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $param["handle_time"]=time();
        $result = $row->allowField(true)->save($param);
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were updated'));
        }
    }

}
